import 'package:aone_common/common.dart';
import 'package:get/get.dart';
import 'package:flutter/material.dart';

class VNavigationBarController extends GetxController
    with GetTickerProviderStateMixin {

  static VNavigationBarController get to => Get.find();

  final UserService _userService = Get.find<UserService>();

  final RxString _currentKey = ''.obs;

  get currentKey => _currentKey.value;

  set currentKey(value) => _currentKey.value = value;

  late Map<String, AnimationController> controllers;

  @override
  void onInit() {
    super.onInit();
    controllers = {
      Routes.home: AnimationController(vsync: this),
      Routes.activity: AnimationController(vsync: this),
      'service': AnimationController(vsync: this),
      Routes.user: AnimationController(vsync: this),
    };
    controllers[Routes.home]!
      ..duration = const Duration(milliseconds: 2000)
      ..forward();
    currentKey = Routes.home;

  }

  void toggleLottie(String key) {
    controllers[key]!
      ..duration = const Duration(milliseconds: 2000)
      ..forward();
    controllers[currentKey]
      ?..duration = const Duration(milliseconds: 1500)
      ..reverse();
    currentKey = key;
  }

  void onTap(String key) async {
    if (key == currentKey) {
      return;
    }
    toggleLottie(key);

    if (key == Routes.user &&
        _userService.state.authLevel == AuthLevel.visitor) {
      Get.toNamed(Routes.login);
      return;
    }

    if (key != 'service') {
      Get.toNamed(key, id: 1, preventDuplicates: true);
    }
  }
}
